/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemCommandListener;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.*;

/**
 * @author hrtz
 */
public class Midlet extends MIDlet implements CommandListener {

    private Display display;
    private SplashScreen splash;
    private Form formSekolah;
    private Form formDataGuru;
    private TextField tfNamaSekolah;
    private TextField tfJumlahGuru;
    private TextField tfNamaGuru[];
    private TextField tfMatpel[];
    private TextField tfJumlahJamPerMingg[];
    private TextField tfJumlahKelasPerMingg[];
    private Command cmdOk = new Command("Ok", Command.OK, 1);
    private Command cmdExit = new Command("Exit", Command.EXIT, 0);
    private ChoiceGroup cgTingkatSekolah;
    String pilihanTingkatSekolah[] = {"SD / MI", "SMP / MTS", "SMA / SMK / MA"};

    public void startApp() {
        display = Display.getDisplay(this);
        display.setCurrent(new SplashScreen(this));
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void displayFormDataGuru(String namaSekolah, int jumlahGuru) {
        if (formDataGuru == null) {
            formDataGuru = new Form("Data guru " + namaSekolah);
            tfNamaGuru = new TextField[jumlahGuru];
            tfMatpel = new TextField[jumlahGuru];
            tfJumlahJamPerMingg = new TextField[jumlahGuru];
            tfJumlahKelasPerMingg = new TextField[jumlahGuru];
            for (int i = 0; i < jumlahGuru; i++) {

                tfNamaGuru[i] = new TextField("Nama guru : ", "", 20, TextField.ANY);
                tfMatpel[i] = new TextField("Mata pelajaran", "", 20, TextField.ANY);
                tfJumlahJamPerMingg[i] = new TextField("Jumlah jam perminggu", "", 3, TextField.NUMERIC);
                tfJumlahKelasPerMingg[i] = new TextField("Jumlah kelas perminggu", "", 3, TextField.NUMERIC);
                formDataGuru.append("Data guru ke "+i);
                formDataGuru.append(tfNamaGuru[i]);
                formDataGuru.append(tfMatpel[i]);
                formDataGuru.append(tfJumlahJamPerMingg[i]);
                formDataGuru.append(tfJumlahKelasPerMingg[i]);
            }
        }
        display.setCurrent(formDataGuru);
    }

    public void displaySplash() {
        splash = new SplashScreen(this);
        display.setCurrent(splash);
    }

    void displayMainMenu() {
        if (formSekolah == null) {
            formSekolah = new Form("Masukkan data sekolah anda");
            formSekolah.setCommandListener(this);
            cgTingkatSekolah = new ChoiceGroup("Pilih Tingkatan Sekolah", ChoiceGroup.POPUP,
                    pilihanTingkatSekolah, null);
            tfNamaSekolah = new TextField("Masukkan nama sekolah", null, 40, TextField.ANY);
            tfJumlahGuru = new TextField("Masukkan Jumlah Guru", null, 3, TextField.NUMERIC);
            formSekolah.append(tfNamaSekolah);
            formSekolah.append(cgTingkatSekolah);
            formSekolah.append(tfJumlahGuru);
            formSekolah.addCommand(cmdOk);
            formSekolah.addCommand(cmdExit);
        }
        display.setCurrent(formSekolah);
    }

    public void commandAction(Command c, Displayable d) {
        if(c == cmdOk){
            displayFormDataGuru(tfNamaSekolah.getString(),
                    Integer.parseInt(tfJumlahGuru.getString()));
        }
        if(c == cmdExit){
            notifyDestroyed();
        }
    }
}

